<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <!-- 搜索框区域 -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="orderNum">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表表格 -->
      <el-table :data="orderlist" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status==='0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间"></el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button plain size="mini" type="primary" icon="el-icon-edit" @click="showDialog"></el-button>
            <el-button
              plain
              size="mini"
              type="success"
              icon="el-icon-location-outline"
              @click="showTime"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 修改地址的弹框 -->
    <el-dialog title="修改地址" :visible.sync="editAddressVisible" @close="resetEditDialog">
      <el-form
        :model="changeAddressForm"
        :rules="changeAddressFormRules"
        ref="changeAddressFormRulesRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address">
          <el-cascader v-model="changeAddressForm.address" :options="cityData"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="addressInfo">
          <el-input v-model="changeAddressForm.addressInfo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddressVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 获取物流进度的对话框 -->
    <el-dialog title="物流进度" :visible.sync="processDialogVisible" width="50%">
      <!-- 时间线组件 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in processList"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
      <span slot="footer">
        <el-button @click="processDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="processDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import citydata from './citydata.js'
export default {
  name: 'orderList',
  data() {
    return {
      processList: [], // 存储的是后台所有返回的物流信息列表
      processDialogVisible: false, // 控制的物流信息默认的可见性
      // 存储的是级联选择器选择的数据
      cityData: citydata,
      // 存储的是编辑表单数据的数据
      changeAddressForm: {
        address: [],
        addressInfo: ''
      },
      // 存储的是编辑表单数据的验证规则
      changeAddressFormRules: {
        address: [{ required: true, message: '请输入省市区', trigger: 'blur' }],
        addressInfo: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      editAddressVisible: false, // 显示修改地址弹框可见性
      total: 0, // 存储的是总条数
      orderlist: [], // 存储所有订单列表数据
      orderNum: '',
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      }
    }
  },
  methods: {
    // 显示物流进度对话框的可见性
    async showTime() {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      console.log(res.data)
      this.processList = res.data
      this.processDialogVisible = true
    },

    // 当城市名称改变时
    handleCityChange() {},

    // 重置表单数据
    resetEditDialog() {
      this.$refs.changeAddressFormRulesRef.resetFields()
    },

    // 显示修改编辑弹框的可见性
    showDialog() {
      this.editAddressVisible = true
    },

    // 每页显示的条目数改变时触发
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 当前页先显示的条目数改变出发
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getOrderList()
    },

    // 获取订单列表数据
    async getOrderList() {
      const { data: res } = await this.$http.get('/orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      // 利用dayjs对createtime进行格式化
      // res.data.goods.forEach(item=>{
      // item.create_time=this.$dayjs(item.create_time).format(格式)
      // })
      res.data.goods.forEach(item => {
        item.create_time = this.$dayjs(item.create_time).format('YYYY-MM-DD hh:ss:mm')
      })
      this.orderlist = res.data.goods
      this.total = res.data.total
    }
  },
  created() {
    this.getOrderList()
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
